package city.spring.service.mealService.impl;

import city.spring.domain.entity.mealService.SendAreaEntity;
import city.spring.modules.bed.entity.BedRoomEntity;
import city.spring.modules.bed.service.BedRoomService;
import city.spring.repository.mealService.SendAreaRepository;
import city.spring.service.mealService.SendAreaService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class SendAreaServiceImpl extends ServiceImpl<SendAreaRepository, SendAreaEntity> implements SendAreaService {

    private final BedRoomService bedRoomService;

    public SendAreaServiceImpl(BedRoomService bedRoomService) {
        this.bedRoomService = bedRoomService;
    }

    @Override
    public void saveSendArea(SendAreaEntity entity) {
        boolean save = save(entity);
        if (!save) {
            throw new RuntimeException("保存送餐区域失败");
        }
    }

    @Override
    public void updateSendArea(SendAreaEntity entity) {
        SendAreaEntity oldEntity = getById(entity.getId());
        boolean update = lambdaUpdate()
                .set(SendAreaEntity::getAreaName, entity.getAreaName())
                .set(SendAreaEntity::getAreaCode, entity.getAreaCode())
                .set(SendAreaEntity::getGmtCreate, entity.getGmtCreate())
                .set(SendAreaEntity::getGmtModified, entity.getGmtModified())
                .eq(SendAreaEntity::getId, entity.getId())
                .update();
        if (!update) {
            throw new RuntimeException("修改送餐区域失败");
        }
        if (!StringUtils.equals(oldEntity.getAreaCode(), entity.getAreaCode())) {
            // 数据字典类型已经被修改，需要手动维护原来旧的类型值列表
            bedRoomService.lambdaUpdate()
                    .set(BedRoomEntity::getAreaId, entity.getAreaCode())
                    .eq(BedRoomEntity::getAreaId, oldEntity.getAreaCode())
                    .update();
        }
    }

    @Override
    public void deleteSendArea(String primaryKey) {
        SendAreaEntity entity = this.getById(primaryKey);
        this.loadBedRoom(entity);
        List<BedRoomEntity> entities = entity.getChildren();
        for (int i = 0; i < entities.size(); i++) {
            bedRoomService.deleteSendId(entities.get(i).getId());
        }
        removeById(primaryKey);
    }

    @Override
    public void deleteSendArea(List<String> primaryKeys) {
        removeByIds(primaryKeys);
//        bedRoomService.lambdaUpdate()
//                .in(BedRoomEntity::getAreaId, primaryKeys)
//                .remove();
    }

    @Override
    public void loadBedRoom(SendAreaEntity entity) {
        entity.setChildren(bedRoomService.getSendAreaBedRoom(entity.getId()));
    }

    @Override
    public void loadBedRoom(List<SendAreaEntity> entities) {
        entities.forEach(this::loadBedRoom);
    }

    @Override
    public void updateSendAreaRoom(SendAreaEntity entity) {
        SendAreaEntity oldEntity = getById(entity.getId());
        this.loadBedRoom(oldEntity);
        if (oldEntity.getChildren() != null) {
            List<BedRoomEntity> oldRoomEntities = oldEntity.getChildren();
            for (int i = 0; i < oldRoomEntities.size(); i++) {
                bedRoomService.deleteSendId(oldRoomEntities.get(i).getId());
            }
        }
        List<BedRoomEntity> newRoomEntities = entity.getChildren();
        for (int j = 0; j < newRoomEntities.size(); j++) {
            bedRoomService.lambdaUpdate().set(BedRoomEntity::getAreaId, entity.getId()).eq(BedRoomEntity::getId, newRoomEntities.get(j).getId()).update();
        }
    }

}
